<template>
  <div class="fillContainer">
    <div>
      <el-form :inline="true" ref="add_data">
        <!-- 筛选 -->
        <el-form-item label="按照时间筛选：">
          <el-date-picker
            v-model="search_data.startTime"
            type="datetime"
            placeholder="选择开始时间"
          >
          </el-date-picker>
          <el-date-picker
            v-model="search_data.endTime"
            type="datetime"
            placeholder="选择结束时间"
          >
          </el-date-picker>
        </el-form-item>
        <!-- 筛选按钮 -->
        <el-form-item>
          <el-button
            type="primary"
            size="small"
            icon="view"
            @click="handleSearch()"
            >筛选</el-button
          >
        </el-form-item>
        <!-- 添加按钮 -->
        <el-form-item class="btnRight">
          <el-button
            type="primary"
            size="small"
            icon="view"
            @click="handleAdd()"
            v-if="user.identity == 'manager'"
            >添加</el-button
          >
        </el-form-item>
      </el-form>
    </div>
    <el-table
      v-if="tableData.length > 0"
      :data="tableData"
      border
      max-height="500"
      style="width: 100%"
    >
      <el-table-column
        label="序号"
        type="index"
        align="center"
        width="50"
      ></el-table-column>

      <el-table-column align="center" prop="date" label="创建时间" width="210">
        <template slot-scope="scope">
          <i class="el-icon-time"></i>
          <span style="margin-left: 10px">{{ scope.row.date }}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" prop="type" label="收支类型">
      </el-table-column>

      <el-table-column
        align="center"
        prop="describe"
        label="收支描述"
      ></el-table-column>

      <el-table-column align="center" prop="income" label="收入">
        <template slot-scope="scope">
          <span style="color: #00d053">{{ scope.row.income }}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" prop="expend" label="支出">
        <template slot-scope="scope">
          <span style="color: #f56767">{{ scope.row.expend }}</span>
        </template>
      </el-table-column>

      <el-table-column align="center" prop="cash" label="账户现金">
        <template slot-scope="scope">
          <span style="color: #4db3ff">{{ scope.row.cash }}</span>
        </template>
      </el-table-column>

      <el-table-column
        align="center"
        prop="remark"
        label="备注"
      ></el-table-column>
      
      <el-table-column
        label="操作"
        align="center"
        prop="operation"
        width="180"
        v-if="user.identity == 'manager'"
      >
        <template slot-scope="scope">
          <el-button
            size="small"
            type="warning"
            @click="handleEdit(scope.$index, scope.row)"
            >编辑</el-button
          >
          <el-button
            size="small"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <el-row>
      <el-col :span="24">
        <div class="paginations">
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page.sync="paginations.page_index"
            :page-sizes="paginations.page_sizes"
            :page-size="paginations.page_size"
            :layout="paginations.layout"
            :total="paginations.total"
          >
          </el-pagination>
        </div>
      </el-col>
    </el-row>
    <Dialog :dialog="dialog" :formData="formData" @update="getProfile"></Dialog>
  </div>
</template>

<script>
import Dialog from '../components/Dialog.vue';
export default {
  name: 'fundList',
  data () {
    return {
      paginations: {
        page_index: 1,   //当前位于哪页
        total: 0,        //总数
        page_size: 5,    //一页显示多少条
        page_sizes: [5, 10, 15, 20],    //每页显示多少条
        layout: "total, sizes, prev, pager, next, jumper"    //翻页属性
      },
      search_data: {
        startTime: '',
        endTime: ''
      },
      filterTableData: [],
      tableData: [],
      allTableDate: [],
      formData: {
        type: '',
        describe: '',
        income: '',
        expend: '',
        cash: '',
        remark: '',
        id: ''
      },
      dialog: {
        show: false,
        title: '',
        option: 'edit'
      }
    }
  },
  components: { Dialog },
  created () {
    this.getProfile()
  },
  computed: {
    user () {
      return this.$store.getters.user
    }
  },
  methods: {
    getProfile () {
      //获取表格数据
      this.$axios.get('/api/profiles')
        .then(res => {
          this.allTableDate = res.data
          this.filterTableData = res.data
          //设置分页数据
          this.setPaginations()
        })
    },
    setPaginations () {
      //分页属性设置
      this.paginations.total = this.allTableDate.length
      this.paginations.page_index = 1
      this.paginations.page_size = 5
      //设置默认的分页数据
      this.tableData = this.allTableDate.filter((item, index) => {
        return index < this.paginations.page_size
      })
    },
    handleEdit (index, row) {
      //编辑
      this.dialog = {
        show: true,
        title: '修改资金信息',
        option: 'edit'
      }
      this.formData = {
        type: row.type,
        describe: row.describe,
        income: row.income,
        expend: row.expend,
        cash: row.cash,
        remark: row.remark,
        id: row._id
      }
    },
    handleDelete (index, row) {
      this.$axios.delete(`/api/profiles/delete/${row._id}`)
        .then(res => {
          this.$message.success('删除成功')
          console.log(res)
          this.getProfile()
        })
    },
    handleAdd () {
      this.dialog = {
        show: true,
        title: '添加资金信息',
        option: 'add'
      }
      this.formData = {
        type: '',
        describe: '',
        income: '',
        expend: '',
        cash: '',
        remark: '',
        id: ''
      }
    },
    handleSizeChange (page_size) {
      //切换size
      this.paginations.page_index = 1
      this.paginations.page_size = page_size
      this.tableData = this.allTableDate.filter((item, index) => {
        return index < page_size
      })
    },
    handleCurrentChange (page) {
      //获取当前页
      let index = this.paginations.page_size * (page - 1)
      //数据的总数
      let nums = this.paginations.page_size * page
      //容器
      let tables = []

      for (let i = index; i < nums; i++) {
        if (this.allTableDate[i]) {
          tables.push(this.allTableDate[i])
        }
        this.tableData = tables
      }
    },
    handleSearch () {
      //筛选
      if (!this.search_data.startTime || !this.search_data.endTime) {
        this.$message.warning('请选择时间区间')
        this.getProfile()
        return
      }

      const sTime = this.search_data.startTime.getTime()
      const eTime = this.search_data.endTime.getTime()
      this.allTableDate = this.filterTableData.filter(item => {
        let date = new Date(item.date)
        let time = date.getTime()
        return time >= sTime && time <= eTime
      })
      //分页数据
      this.setPaginations()
    }
  },
}
</script>

<style scoped>
.fillContainer {
  width: 100%;
  height: 100%;
  padding: 20px;
  box-sizing: border-box;
  overflow: hidden;
}
.btnRight {
  float: right;
  margin-right: 20px;
}
.paginations {
    text-align: right;
  margin-top: 10px;
  margin-right: 20px;
}

</style>